// ignore_for_file: unnecessary_null_comparison
import 'package:aitao_flutter/utils/alert_utils.dart';
import 'package:aitao_flutter/utils/cache_utils.dart';
import 'package:aitao_flutter/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../const/common_const.dart';
import '../../routes/routes.dart';

class SettingPageController extends GetxController {
  List<Map> tabListData = [
    {
      'title': MineStringConst.bingCountText,
      'des': SpUtil.getLoginUserInfo().mobile
    },
    {'title': MineStringConst.updatePasswordText, 'des': ''},
    {'title': MineStringConst.youngText, 'des': ''},
    {'title': MineStringConst.blackListText, 'des': ''},
    {'title': MineStringConst.newMessageNotificationText, 'des': ''},
    {'title': MineStringConst.cleanCacheText, 'des': '0M'},
    {'title': MineStringConst.cleanChatRecordsText, 'des': ''},
    {'title': MineStringConst.aboutAppText, 'des': ''},
    {'title': MineStringConst.languageText, 'des': ''},
  ];

  /// 退出登录
  void logoutClick() {
    AlertUtils().showCenterTipsDialog(Get.context!, (e) {
      SpUtil.cleanUserInfo();
      Get.offAllNamed(Routes.smsCodeLoginPage);
    }, '退出登录', '是否退出当前账号');
  }

  void cellClick(String title) {
    if (title == MineStringConst.blackListText) {
      Get.toNamed(Routes.blackListPage);
    } else if (title == MineStringConst.aboutAppText) {
      Get.toNamed(Routes.aboutPage);
    } else if (title == MineStringConst.newMessageNotificationText) {
      Get.toNamed(Routes.messageNotiPage);
    } else if (title == MineStringConst.updatePasswordText) {
      Get.toNamed(Routes.changePasswordPage);
    } else if (title == MineStringConst.cleanCacheText) {
      cleanCacheClick();
    } else if (title == MineStringConst.youngText) {
      Get.toNamed(Routes.youngSetPage);
    } else if (title == MineStringConst.languageText) {
      Get.toNamed(Routes.languageSetPage);
    } else {
      debugPrint(title);
    }
  }

  void cleanCacheClick() {
    AlertUtils().showCenterTipsDialog(Get.context!, (e) {
      CacheUtil.clear().then((value) {
        EasyLoading.showToast('清除缓存成功');
        getCacheData();
      });
    }, MineStringConst.cleanCacheText, '是否清除缓存');
  }

  @override
  void onInit() {
    super.onInit();
    getCacheData();
  }

  void getCacheData() async {
    CacheUtil.total().then((value) {
      tabListData[5]['des'] = value;
      update();
    });
  }
}
